<?php


namespace app\common\library;

use common;
use think\Db;

class MonitorModel {
    /*
     * 差车1-7，禁停区，车站，自由停放，服务区外车辆数量，电量，电压
     */

    function pick_bike_state($city_id) {
        $map = [
            "bike_repark_index_0" => 0,
            "bike_repark_index_1" => 0,
            "bike_repark_index_2" => 0,
            "bike_repark_index_3" => 0,
            "bike_repark_index_4" => 0,
            "bike_repark_index_5" => 0,
            "bike_repark_index_6" => 0,
            "bike_repark_index_7" => 0,
            "bike_repark_index_8" => 0,
            "bike_park_state_free" => 0,
            "bike_park_state_park" => 0,
            "bike_park_state_npa" => 0,
            "bike_park_state_osva" => 0,
            "bike_bus_state_idle" => 0,
            "bike_bus_state_ride" => 0,
            "bike_bus_state_repark" => 0,
            "bike_bus_state_battery" => 0,
            "bike_battery_level_0" => 0,
            "bike_battery_level_10" => 0,
            "bike_battery_level_30" => 0,
            "bike_battery_level_50" => 0,
            "bike_battery_level_70" => 0,
            "bike_battery_level_90" => 0,
            "bike_battery_level_100" => 0,
            'bike_battery_level_out' => 0,
            'bike_total' => 0,
            'bike_offline' => 0,
            'bike_alert_no_gps' => 0,
            'bike_alert_no_scaned' => 0,
            'bike_alert_no_bus' => 0,
            'bike_alert_fault' => 0,
        ];

        //stat bikes start
        $rows = Db::table('tb_bike')->where(['city_id'=>$city_id,'is_deleted'=>0])->field(["alert_state", "bus_state", "repark_index", "park_state", "battery_level", "is_under_volt", "is_online"])->select();

        $map['bike_total'] = count($rows);

        foreach ($rows as $row) {
            $battery_level = intval($row['battery_level']);
            if ($battery_level >= 100) {
                $map['bike_battery_level_100'] ++;
            } else if ($battery_level >= 90) {
                $map['bike_battery_level_90'] ++;
            } else if ($battery_level >= 70) {
                $map['bike_battery_level_70'] ++;
            } else if ($battery_level >= 50) {
                $map['bike_battery_level_50'] ++;
            } else if ($battery_level >= 30) {
                $map['bike_battery_level_30'] ++;
            } else if ($battery_level >= 10) {
                $map['bike_battery_level_10'] ++;
            } else if ($battery_level >= 0) {
                $map['bike_battery_level_0'] ++;
            }
            if ($row['is_under_volt'] == 1) {
                $map['bike_battery_level_out'] ++;
            }

            $repark_index = intval($row['repark_index']);
            $map["bike_repark_index_{$repark_index}"] ++;

            $bus_state = intval($row['bus_state']);
            if ($bus_state == BIKE_BUS_STAT_IDLE) {
                $map['bike_bus_state_idle'] ++;
            } else if ($bus_state == BIKE_BUS_STAT_RIDING || $bus_state == BIKE_BUS_STAT_TEMP_PARK) {
                $map['bike_bus_state_ride'] ++;
            } else if ($bus_state == BIKE_BUS_STAT_OPER_REPARK) {
                $map['bike_bus_state_repark'] ++;
            } else if ($bus_state == BIKE_BUS_STAT_BATTERY_CHANGE) {
                $map['bike_bus_state_battery'] ++;
            }

            $park_state = intval($row['park_state']);
            if ($park_state == BIKE_PARK_STAT_FREE) {
                $map['bike_park_state_free'] ++;
            } else if ($park_state == BIKE_PARK_STAT_PARK) {
                $map['bike_park_state_park'] ++;
            } else if ($park_state == BIKE_PARK_STAT_NPA) {
                $map['bike_park_state_npa'] ++;
            } else if ($park_state == BIKE_PARK_STAT_OSA) {
                $map['bike_park_state_osva'] ++;
            }

            if ($row['is_online'] == 0) {
                $map['bike_offline'] ++;
            }

            $alert_state = intval($row['alert_state']);
            if ($alert_state & BIKE_ALERT_GPS_FAILED) {
                $map['bike_alert_no_gps'] ++;
            }

            if ($alert_state & BIKE_ALERT_LONG_NO_SCANED) {
                $map['bike_alert_no_scaned'] ++;
            }
            if ($alert_state & BIKE_ALERT_LONG_NO_BUS) {
                $map['bike_alert_no_bus'] ++;
            }
            if ($alert_state & BIKE_ALERT_FAULT) {
                $map['bike_alert_fault'] ++;
            }
        }

        $tags = [
            'city_id' => $city_id,
        ];

        Hiflux::insert('monitor_bike_stat', $tags, $map);

        //stat bikes end
        //stat orders
    }

    function pick_park($city_id) {

        $parkstatmap = [
        ];
        $rows = Db::table('tb_park')->where(['city_id'=>$city_id,'is_deleted'=>0])->field(['id','bike_count'])->select();
        foreach ($rows as $row) {
            $parkstatmap[$row['id']] = [
                'order_count' => 0,
                'bike_idle_time_total' => 0,
                'bike_count' => intval($row['bike_count']),
            ];
        }

        $nowtimestamp = strtotime("-1 hour");
        $starttimestamp = $nowtimestamp - $nowtimestamp % 3600;

       $beans = [
            'city_id'=>$city_id,
            'create_time' => [['>=',date('Y-m-d H:00:00', $nowtimestamp)],['<=', date('Y-m-d H:59:59', $nowtimestamp)],'and']
        ];
        $rows = Db::table('tb_user_ride_order')->where($beans)->field(['start_park_id','bike_idle_time'])->select();
        foreach ($rows as $row) {
            $park_id = $row['start_park_id'];
            if (empty($park_id)) {
                $park_id = 'FREE';
            }
            if (!isset($parkstatmap[$park_id])) {
                $parkstatmap[$park_id] = [
                    'order_count' => 0,
                    'bike_idle_time_total' => 0,
                    'bike_count' => 0,
                ];
            }

            $parkstatmap[$park_id]['order_count'] ++;
            $parkstatmap[$park_id]['bike_idle_time_total'] += intval($row['bike_idle_time']);
        }

        $points = [];
        foreach ($parkstatmap as $id => &$e) {
            if ($e['order_count'] > 0) {
                $e['bike_idle_time_total'] = $e['bike_idle_time_total'] / $e['order_count'];
            }

            $tags = [
                'city_id' => $city_id,
                'id' => $id,
            ];

            $points[] = [
                'tags' => $tags,
                'fields' => $e,
                'ns' => $starttimestamp * 1000000000,
            ];
        }

        Hiflux::insert_muilt('monitor_park', $points);
    }

    function query_bike_stat($city_id, $start_time, $end_time, $pos, $limit) {
        $measurement = 'monitor_bike_stat';
        $selectlist = [
            "bike_repark_index_0" => 'int',
            "bike_repark_index_1" => 'int',
            "bike_repark_index_2" => 'int',
            "bike_repark_index_3" => 'int',
            "bike_repark_index_4" => 'int',
            "bike_repark_index_5" => 'int',
            "bike_repark_index_6" => 'int',
            "bike_repark_index_7" => 'int',
            "bike_repark_index_8" => 'int',
            "bike_park_state_free" => 'int',
            "bike_park_state_park" => 'int',
            "bike_park_state_npa" => 'int',
            "bike_park_state_osva" => 'int',
            "bike_bus_state_idle" => 'int',
            "bike_bus_state_ride" => 'int',
            "bike_bus_state_repark" => 'int',
            "bike_bus_state_battery" => 'int',
            "bike_battery_level_0" => 'int',
            "bike_battery_level_10" => 'int',
            "bike_battery_level_30" => 'int',
            "bike_battery_level_50" => 'int',
            "bike_battery_level_70" => 'int',
            "bike_battery_level_90" => 'int',
            "bike_battery_level_100" => 'int',
            'bike_battery_level_out' => 'int',
            'bike_total' => 'int',
            'bike_offline' => 'int',
            'bike_alert_no_gps' => 'int',
            'bike_alert_no_scaned' => 'int',
            'bike_alert_no_bus' => 'int',
            'bike_alert_fault' => 'int',
            'time' => 'string',
        ];

        $where = " where city_id = '{$city_id}' and time >= '{$start_time}' and time <= '{$end_time}' order by time desc ";

        $l = Hiflux::query($measurement, $selectlist, $where, $pos, $limit);
        $ltmp = [];
        foreach ($l as $item) {
            $ltmp[] = common::sql_row($selectlist, $item);
        }
        return $ltmp;
    }

    function query_park($city_id, $park_id, $start_time, $end_time, $pos, $limit) {
        $measurement = 'monitor_park';
        $selectlist = [
            'order_count' => 'int',
            'bike_idle_time_total' => 'int',
            'bike_count' => 'int',
            'time' => 'string',
        ];

        $where = " where city_id = '{$city_id}' and id = '{$park_id}' and  time >= '{$start_time}' and time <= '{$end_time}' order by time desc ";

        $l = Hiflux::query($measurement, $selectlist, $where, $pos, $limit);
        $ltmp = [];
        foreach ($l as $item) {
            $ltmp[] = common::sql_row($selectlist, $item);
        }
        return $ltmp;
    }

}
